<template>
  <Card :bordered="false" dis-hover>
    <jgsearch
      :searchData="searchData"
      @on-enter="askDatasearch"
      @on-clear="askDatasearch"
      @click="askDataclick"
      @on-change-change="askDatachanges"
      @on-change-changeing="askDatachangeings"
      @on-change-changes="askDatachange"
      @on-change-changess="askDatachangess"
    >
      <template v-slot:tag>
        <tags
          @sunchangetag="sunchangetag"
          :tags_ids="params.tags_ids"
          :project_tags_ids="params.project_tags_ids"
        />
      </template>
      <!-- <template v-slot:timeButton>
                <div>
                    <Button @click="timeButtons(10)" :type="timeButton==10?'primary':'default'" :ghost="timeButton==10" class="ivu-mr-8">近10天</Button>
                    <Button @click="timeButtons(30)" :type="timeButton==30?'primary':'default'" :ghost="timeButton==30" class="ivu-mr-8">近30天</Button>
                    <Button @click="timeButtons(90)" :type="timeButton==90?'primary':'default'" :ghost="timeButton==90">近90天</Button>
                </div>
            </template> -->
      <template slot="select">
        <Select
          v-if="showkey"
          @on-change="askDatachangeing"
          placeholder="请选择上次归属人"
          clearable
          v-model="params.belong_member_id"
        >
          <OptionGroup
            :label="item.label"
            v-for="item in member_list_list"
            :key="item.label"
          >
            <Option
              v-for="its in item.children"
              :value="its.value"
              :key="its.value"
              >{{ its.label }}</Option
            >
          </OptionGroup>
        </Select>
      </template>
      <template v-slot:city>
        <!-- <Select
                    v-width="130"
                    v-model="params.area_code"
                    placeholder="请输入城市"
                    filterable
                    remote
                    clearable
                    :remote-method="remoteMethod"
                    @on-change="getcityDatas"
                    :loading="loadingcity"
                >
                    <Option
                        v-for="(item, index) in optionscity"
                        :key="index"
                        :value="item.value"
                        :label="item.label"
                        >{{ item.label }}</Option
                    >
                </Select> -->
        <jgcheckcity
          ref="checkcity"
          @cityvalue="cityvalue"
          v-model="area_code"
          name="publicList"
        />
      </template>
      <template v-slot:addcheck>
        <Row type="flex">
          <!-- <Col>
                        <CheckboxGroup @on-change="checkbox1">
                            <Checkbox label="电话"></Checkbox>
                        </CheckboxGroup>
                    </Col>
                    <Col>
                        <CheckboxGroup @on-change="checkbox2">
                            <Checkbox label="微信"></Checkbox> </CheckboxGroup
                    ></Col> -->
          <Col>
            <Select
              v-model="params.have_repeat"
              placeholder="全部"
              clearable
              @on-change="checkbox3"
            >
              <Option value="1" label="重复">重复</Option>
              <Option value="2" label="不重复">不重复</Option>
            </Select>
          </Col>
        </Row>
      </template>
      <template v-slot:export>
        <Button
          type="primary"
          @click="exportTels"
          :disabled="formItem.allot_ids == ''"
          v-hasC="{
            name: 'v1-patient-index-get-patient-mobile',
            flag: 'v1/patient/index/get-patient-mobile',
          }"
          >复制号码</Button
        >
      </template>
    </jgsearch>
    <div class="i-table-no-border">
      <Table
        highlight-row
        :max-height="tableHight"
        ref="table"
        :columns="columns"
        :data="list"
        :loading="loading"
        size="small"
        @on-sort-change="sortchange"
        @on-selection-change="selecttanle"
        @on-filter-change="handleFilterChange"
      >
        <template slot-scope="{ row }" slot="info">
          <a @click="handleDetailPatient(row)"
            ><Tooltip
              placement="top"
              content="共享数据"
              v-if="+row.is_share_project === 1"
            >
              <Icon type="ios-share" :size="16" /> </Tooltip
            >{{ row.user_name }} {{ genderfun(row.gender) }} {{ row.age }}岁</a
          >
        </template>
        <template slot-scope="{ row }" slot="state">
          <Tag v-if="row.status_name === '无效'" color="red">{{
            row.status_name
          }}</Tag>
          <Tag v-else-if="row.status_name === '跟进中'" color="volcano">{{
            row.status_name
          }}</Tag>
          <Tag v-else-if="row.status_name === '预约'" color="cyan">{{
            row.status_name
          }}</Tag>
          <Tag v-else-if="row.status_name === '到诊'" color="green">{{
            row.status_name
          }}</Tag>
          <Tag v-else-if="row.status_name === '老患者到诊'" color="magenta">{{
            row.status_name
          }}</Tag>
          <Tag v-else-if="row.status_name === '有意向'" color="blue">{{
            row.status_name
          }}</Tag>
          <Tag v-else-if="row.status_name === '未开口'" color="purple">{{
            row.status_name
          }}</Tag>
          <Tag v-else-if="row.status_name" color="orange">{{
            row.status_name
          }}</Tag>
        </template>
        <template slot-scope="{ row }" slot="label">
          <Poptip word-wrap trigger="hover" placement="right" width="400">
            <template v-for="(item, index) in row.tags_name">
              <Tag
                v-show="index < 3"
                :color="item.type == 1 ? 'warning' : 'primary'"
                :key="index"
                >{{ item.tags_name.replace(/[\r\n]/g, "") }}</Tag
              >
            </template>
            <div slot="content">
              <template v-for="(item, index) in row.tags_name">
                <Tag
                  :color="item.type == 1 ? 'warning' : 'primary'"
                  :key="index"
                  >{{ item.tags_name.replace(/[\r\n]/g, "") }}</Tag
                >
              </template>
            </div>
          </Poptip>
        </template>
        <template slot-scope="{ row }" slot="channel">
          <Badge
            v-if="row.source_name === 0"
            status="success"
            :text="row.source_name"
          />
          <Badge
            v-if="row.source_name === 1"
            status="processing"
            :text="row.source_name"
          />
          <Badge
            v-if="row.source_name === 2"
            status="error"
            :text="row.source_name"
          />
          <Badge
            v-if="row.source_name === 3"
            status="warning"
            :text="row.source_name"
          />
          <span>{{ row.source_name }}</span>
        </template>
        <template slot-scope="{ row }" slot="disease">
          <span>{{ row.child_diseases_name || row.diseases_name }}</span>
        </template>
        <template slot-scope="{ row, index }" slot="action">
          <a
            v-hasC="{
              name: 'patient-index-update5',
              flag: 'v1/patient/index/update',
            }"
            @click="handleOpenDrawer(row)"
            >编辑</a
          >
          <Divider
            type="vertical"
            v-hasC="{
              name: 'patient-index-update5',
              flag: 'v1/patient/index/update',
            }"
          />
          <a
            v-hasC="{
              name: 'patient-index-update5',
              flag: 'v1/patient/index/update',
            }"
            @click="handleReturnVisit(row.id)"
            >回访</a
          >
          <Divider
            type="vertical"
            v-if="+row.repeat_type > 0"
            v-hasC="{
              name: 'v1-patient-index-repeat-list3',
              flag: 'v1/patient/index/repeat-list',
            }"
          />
          <a
            @click="copypatient(row)"
            v-hasC="{
              name: 'v1-patient-index-repeat-list3',
              flag: 'v1/patient/index/repeat-list',
            }"
            v-if="+row.repeat_type > 0"
            >重复</a
          >
          <Divider
            type="vertical"
            v-if="+row.is_delete === 1"
            v-hasC="btnShows"
          />
          <a
            @click="delpatient(row)"
            v-hasC="btnShows"
            style="color: red;"
            v-if="+row.is_delete === 1"
            >删除</a
          >
        </template>
      </Table>
      <div
        class="ivu-mt iuv-text-center"
        slot="footer"
        style="text-align: center;"
      >
        <Page
          show-total
          show-elevator
          show-sizer
          :total="total"
          :current.sync="params.page"
          :page-size="params.pageSize"
          :page-size-opts="pageOpts"
          @on-change="changepage"
          @on-page-size-change="changepageSize"
        />
      </div>
      <patient-detail
        ref="patientDetail"
        :btnShow="btnShow"
        @cancelDrawer="askData"
        @askData="askData"
      />
      <edit-patient ref="editPatient" @patientfun="askData" v-if="!loading" />
      <!-- <return-visit ref="returnVisit" /> -->
      <return-visit ref="returnVisit" @patientfuns="askData" />
    </div>
    <Modal v-model="modal" title="患者分配" @on-cancel="handleDrawerCancel">
      <Select v-model="formItem.allot_member_ids" filterable multiple>
        <OptionGroup
          :label="item.label"
          v-for="item in userList"
          :key="item.value"
        >
          <Option
            v-show="+its.status === 1"
            v-for="its in item.children"
            :value="its.value"
            :key="its.value"
            >{{ its.label }}</Option
          >
        </OptionGroup>
      </Select>
      <div slot="footer">
        <Button @click="handleDrawerCancel">取消</Button>
        <Button type="primary" :loading="remarksloading" @click="handleSubmit"
          >确定</Button
        >
      </div>
    </Modal>
    <copymodal ref="copymodal" @askData="askData" :btnShow="btnShows" />
  </Card>
</template>
<script>
import jgsearch from "@/components/search";
// import jgcheckcity from "@/components/checkcity";
import mixincity from "@/mixins/mixincity";

import patientDetail from "@/components/patientDetail";
import editPatient from "@/components/editPatient";
import returnVisit from "@/components/returnVisit";
import copymodal from "@/pages/patient/copymodal";
import {
  patientindexsealist,
  patientindexseaallot,
} from "@/api/hqapi/indexone.js";
import tags from "@/components/tags";
import tagmixins from "@/mixins/tagmixins";
import {
  patientindexsearchcity,
  patientindexdelete,
  customerwechattagslist,
  patientindexseacondition,
  gePatientMobile,
} from "@/api/hqapi";
export default {
  name: "publicList",
  components: {
    jgsearch,
    patientDetail,
    editPatient,
    returnVisit,
    copymodal,
    tags,
  },
  mixins: [mixincity, tagmixins],
  data() {
    return {
      timeButton: null,
      btnShows: {
        name: "v1-patient-index-delete2",
        flag: "v1/patient/index/delete",
      },
      loading: false,
      btnShow: [
        {
          name: "patient-index-sub1",
          flag: "v1/patient/index/sub",
        },
        {
          name: "patient-index-edit-remark3",
          flag: "v1/patient/index/edit-remark",
        },
        {
          name: "patient-index-update1",
          flag: "v1/patient/index/update",
        },
        {
          name: "patient-callback-callback-common-list5",
          flag: "v1/patient/callback/callback-common-list",
        },
        {
          name: "patient-index-edit-tags2",
          flag: "v1/patient/index/edit-tags",
        },
        {
          name: "patient-index-edit-tags3",
          flag: "v1/patient/index/edit-tags",
        },
      ],
      remarksloading: false,
      loadingcity: false,
      searchData: {
        project: [
          // {
          //     id: 20,
          //     key: 'slot',
          //     keys: 'timeButton'
          // },
          {
            id: 1,
            key: "DatePicker",
            type: "daterange",
            style: { width: "200px" },
            value: ["", ""],
            options: {
              disabledDate(date) {
                return date && date.valueOf() > Date.now();
              },
            },
            placeholder: "请选择导入日期",
            callback: "changeing",
            clearable: true,
          },
          {
            id: 31,
            key: "DatePicker",
            type: "daterange",
            style: { width: "200px" },
            value: ["", ""],
            options: {
              disabledDate(date) {
                return date && date.valueOf() > Date.now();
              },
            },
            placeholder: "请选择创建日期",
            callback: "changeing",
            clearable: true,
          },
          {
            id: 2,
            key: "Input",
            placeholder: "请输入患者姓名/手机号/微信号",
            clearable: true,
            style: { width: "290px" },
            value: "",
          },
          {
            id: 21,
            key: "Select",
            transfer: true,
            style: { minWidth: "100px" },
            placeholder: "电话微信",
            value: "",
            childList: [
              { label: "只有电话", value: 1 },
              { label: "只有微信", value: 2 },
              { label: "无联系方式", value: 3 },
              { label: "有电话有微信", value: 4 },
            ],
            clearable: true,
            callback: "change",
          },

          {
            id: 3,
            key: "TreeSelect",
            placeholder: "来源",
            clearable: true,
            maxTagCount: 3,
            multiple: true,
            value: [],
            childList: [],
            callback: "change",
          },
          {
            id: 7,
            key: "slot",
            keys: "city",
          },
          {
            id: 4,
            key: "Select",
            placeholder: "患者状态",
            style: { width: "100px" },
            clearable: true,
            value: "",
            field: { value: "id", label: "status_name" },
            childList: [],
            callback: "change",
          },
          {
            id: 10,
            key: "TreeSelect",
            placeholder: "病种",
            clearable: true,
            style: { width: "100px" },
            value: "",
            childList: [],
            callback: "change",
          },
          {
            id: 15,
            key: "Select",
            placeholder: "上次回访状态",
            clearable: true,
            style: { width: "120px" },
            value: "",
            childList: [],
            callback: "change",
          },
          {
            id: 11,
            key: "Select",
            placeholder: "选择上次回访人",
            clearable: true,
            value: "",
            childList: [],
            callback: "change",
          },
          {
            id: 6,
            key: "Select",
            placeholder: "选择部门",
            clearable: true,
            value: "",
            childList: [],
            callback: "changes",
          },
          {
            id: 5,
            key: "slot",
            keys: "select",
          },
          {
            id: 12,
            key: "Select",
            transfer: true,
            style: { minWidth: "100px" },
            placeholder: "对话方式",
            value: "",
            childList: [],
            clearable: true,
            callback: "change",
          },

          // {
          //     id: 5,
          //     key: "Select",
          //     // show: { key: false },
          //     placeholder: "选择上次归属人",
          //     Group: true,
          //     clearable: true,
          //     value: "",
          //     childList: [],
          //     callback: "change",
          // },
          { id: 30, key: "slot", keys: "tag" },
          { id: 8, key: "slot", keys: "addcheck" },
          { id: 100, key: "slot", keys: "export" },
        ],
        button: [
          {
            key: "分配",
            has: {
              name: "patient-index-sea-allot2",
              flag: "v1/patient/index/sea-allot",
            },
          },
        ],
      },
      columns: [
        { type: "selection", fixed: "left", width: 55 },
        {
          title: "基本信息",
          key: "basic",
          slot: "info",
          width: 150,
          fixed: "left",
        },
        // { title: '微信号', key: 'wx', width: 108 },
        { title: "患者状态", key: "state", slot: "state", width: 100 },
        { title: "上次归属人", key: "realname", width: 118 },
        {
          title: "导入时间",
          key: "export_time_str",
          sortable: "custom",
          width: 108,
        },
        {
          title: "回访次数",
          key: "callback_count",
          sortable: "custom",
          width: 108,
        },
        {
          title: "上次回访人",
          key: "last_callback_realname",
          width: 108,
        },
        {
          title: "上次回访状态",
          key: "last_callback_state_name",
          width: 108,
        },
        {
          title: "未访天数",
          key: "uncallback_day",
          sortable: "custom",
          filters: [
            {
              label: "0-10天未访",
              value: "1",
            },
            {
              label: "11-20天未访",
              value: "2",
            },
            {
              label: "21-30天未访",
              value: "3",
            },
            {
              label: "31-60天未访",
              value: "4",
            },
            {
              label: "60天以上未访",
              value: "5",
            },
          ],
          filterMultiple: false,
          filterRemote(value) {
            this.filterType = value[0];
          },
          width: 118,
        },
        { title: "患者标签", key: "label", slot: "label", width: 260 },
        { title: "城市", key: "city_name", width: 200 },
        { title: "病种", slot: "disease", width: 200 },
        {
          title: "来源",
          width: 260,
          slot: "channel",
          ellipsis: true,
          tooltip: true,
        },
        // { title: '创建时间', key: 'foundTime', width: 135 },
        {
          title: "备注",
          minWidth: 250,
          key: "remark",
          ellipsis: true,
          tooltip: true,
        },
        { title: "操作", width: 185, slot: "action", fixed: "right" },
      ],
      total: 0,
      pageOpts: [10, 15, 20, 30, 50, 100],
      realnamebool: true,
      params: {
        page: 1,
        pageSize: 10,
        export_stime: "",
        export_etime: "",
        created_stime: "",
        created_etime: "",
        department_id: "",
        last_callback_id: "",
        keywords: "",
        patient_status_id: "",
        channel_id: [],
        have_wechat: 0,
        have_mobile: 0,
        have_repeat: 0,
        area_code: "",
        tags_ids: "",
        project_tags_ids: "",
        belong_member_id: "",
        sort_export_time: "",
        sort_callback_count: "",
        sort_last_callback_time: "",
        not_callback_start: "",
        not_callback_end: "",
        patient_diseases_id: "",
        have_contact_type: "",
        dialog_mode_id: "",
        last_callback_state_id: "",
      },
      condition: {},
      userList: [],
      member_list_list: [],
      showkey: false,
      formItem: { allot_member_ids: [], allot_ids: "" },
      modal: false,
      list: [],
      member_list: [],
      optionscity: [],
      timer: "",
      area_code: [],
      tableHight: document.body.clientHeight - 350,
    };
  },
  mounted() {
    this.initial();
  },
  methods: {
    exportTels() {
      gePatientMobile({ patient_ids: this.formItem.allot_ids }).then((res) => {
        let tels = res.join("\n");
        this.$Copy({
          text: tels,
        });
      });
    },
    cityvalue(city) {
      this.area_code = city;
      this.params.area_code = city.join(",");
      this.params.page = 1;
      this.askData();
    },
    timeButtons(num) {
      this.searchData.project.forEach((item) => {
        if (item.id === 1) {
          item.value = [this.$Date().subtract(num, "day"), this.$Date()];
          this.params.export_stime = item.value[0];
          this.params.export_etime = item.value[1];
          this.timeButton = num;
        }
        if (item.id === 31) {
          // item.value = [this.$Date().subtract(num, 'day'), this.$Date()]
          this.params.created_stime = item.value[0];
          this.params.created_etime = item.value[1];
        }
      });
      this.askData();
    },
    askDatachangeings() {
      this.timeButton = null;
      this.askDatachanges();
    },
    copypatient(row) {
      this.$refs.copymodal.initial(row.id);
    },
    defaultcityname() {
      this.optionscity = JSON.parse(JSON.stringify(this.defaultcity));
    },
    changetag(member_id = "") {
      customerwechattagslist({ member_id }).then((res) => {
        this.searchData.project.forEach((item) => {
          if (item.id === 9) {
            item.value = [];
            item.childList = res.list;
          }
        });
        this.askData();
      });
    },
    initial() {
      this.storage.set("publicList", ["", ""]);
      let attr_list = JSON.parse(window.localStorage.getItem("attr_list"));
      let ProjectId = window.localStorage.getItem("currentProject");
      for (let item of attr_list) {
        if (+item.project_id === +ProjectId) {
          this.searchData.project.forEach((items) => {
            if (items.id === 6) {
              items.value = item.department_id;
              this.params.department_id = item.department_id;
            }
          });
        }
      }
      patientindexseacondition().then((res) => {
        this.member_list = res.condition.member_list;
        this.searchData.project.forEach((item) => {
          if (item.id === 3) {
            if (item.childList.length === 0) {
              item.childList = res.condition.source_channel;
            }
          } else if (item.id === 4) {
            if (item.childList.length === 0) {
              item.childList = res.condition.status_list;
            }
          } else if (item.id === 6) {
            if (item.childList.length === 0) {
              item.childList = res.condition.department_list;
              if (res.condition.department_list.length === 1) {
                this.showkey = true;
                this.member_list_list = res.condition.department_list;
              }
            }
            // if (res.condition.department_list.length === 1)
            //     item.value = res.condition.department_list[0].value;
          } else if (item.id === 10) {
            if (item.childList.length === 0) {
              item.childList = res.condition.diseases;
            }
          } else if (item.id === 11) {
            if (item.childList.length === 0) {
              item.childList = res.condition.last_callback_list;
            }
          } else if (item.id === 15) {
            if (item.childList.length === 0) {
              item.childList = res.condition.last_callback_status_list;
            }
          } else if (item.id === 12) {
            if (item.childList.length === 0) {
              item.childList = res.condition.talk_way;
            }
          }
        });
        this.userList = res.condition.allot_member_list;
      });
      this.timeButtons(10);
    },
    askData() {
      this.loading = true;
      this.formItem = { allot_member_ids: [], allot_ids: "" };
      let params = JSON.parse(JSON.stringify(this.params));
      params.channel_id = params.channel_id.join(",");
      this.$store.commit("admin/account/changeCloseRefresh", false);
      patientindexsealist(params).then((res) => {
        if (this.params.department_id) {
          this.member_list_list = [];
          for (let its in this.member_list) {
            if (this.params.department_id === this.member_list[its].value) {
              this.member_list_list = [this.member_list[its]];
              this.showkey = true;
            }
          }
        } else if (!this.params.department_id) {
          this.showkey = false;
        }
        this.total = res.total;
        this.list = res.list;
        this.loading = false;
      });
    },
    /* @函数功能: 分页 */
    changepage(e) {
      this.params.page = e;
      this.askData();
    },
    /* @函数功能: 每页数量 */
    changepageSize(e) {
      this.params.pageSize = e;
      this.askData();
    },
    delpatient(row) {
      this.$Modal.confirm({
        title: "删除确认",
        content: `<p>删除后患者进入回收站，将不在其他列表展示</p>`,
        loading: true,
        onOk: () => {
          patientindexdelete({ id: row.id }).then(() => {
            this.$Message.info("删除成功");
            this.askData();
            this.$Modal.remove();
          });
        },
      });
    },
    askDatasearch() {
      this.params = {
        page: 1,
        pageSize: 10,
        export_stime: "",
        export_etime: "",
        created_stime: "",
        created_etime: "",
        keywords: "",
        department_id: "",
        patient_diseases_id: "",
        patient_status_id: "",
        tags_ids: "",
        project_tags_ids: "",
        last_callback_id: "",
        channel_id: [],
        belong_member_id: "",
        sort_export_time: "",
        sort_callback_count: "",
        sort_last_callback_time: "",
        not_callback_start: "",
        not_callback_end: "",
        have_contact_type: "",
        dialog_mode_id: "",
        area_code: "",
      };
      this.searchData.project.forEach((item) => {
        if (item.id === 2) {
          this.params.keywords = item.value;
        } else if (item.id === 1) {
          let keywording = "";
          this.searchData.project.forEach((items) => {
            if (items.id === 2) {
              keywording = items.value;
            }
          });
          if (keywording !== "") {
            if (JSON.stringify(item.value) !== JSON.stringify(["", ""])) {
              let timer = [
                this.$Date(item.value[0]).format("YYYY-MM-DD"),
                this.$Date(item.value[1]).format("YYYY-MM-DD"),
              ];
              this.storage.set("publicList", timer);
              item.value = ["", ""];
              this.params.export_stime = "";
              this.params.export_etime = "";
            }
          } else {
            if (
              JSON.stringify(this.storage.get("publicList")) !==
              JSON.stringify(["", ""])
            ) {
              item.value = this.storage.get("publicList");
              this.params.export_stime = item.value[0];
              this.params.export_etime = item.value[1];
            }
          }
        } else if (item.id === 31) {
          let keywording = "";
          this.searchData.project.forEach((items) => {
            if (items.id === 2) {
              keywording = items.value;
            }
          });
          if (keywording !== "") {
            if (JSON.stringify(item.value) !== JSON.stringify(["", ""])) {
              let timer2 = [
                this.$Date(item.value[0]).format("YYYY-MM-DD"),
                this.$Date(item.value[1]).format("YYYY-MM-DD"),
              ];
              this.storage.set("publicList2", timer2);
              item.value = ["", ""];
              this.params.created_stime = "";
              this.params.created_etime = "";
            }
          } else {
            if (
              JSON.stringify(this.storage.get("publicList2")) !==
              JSON.stringify(["", ""])
            ) {
              item.value = this.storage.get("publicList2");
              this.params.created_stime = item.value[0];
              this.params.created_etime = item.value[1];
            }
          }
        } else if (item.id === 3) {
          item.value = [];
        } else if (item.id === 4) {
          item.value = "";
        } else if (item.id === 6) {
          item.value = "";
        } else if (item.id === 9) {
          item.value = [];
          item.childList = [];
        } else if (item.id === 21) {
          item.value = "";
        } else if (item.id === 12) {
          item.value = "";
        }
      });
      this.showkey = false;
      this.askData();
    },
    askDatachanges() {
      this.params.page = 1;
      this.searchData.project.forEach((item) => {
        if (item.id === 1) {
          this.params.export_stime = item.value[0];
          this.params.export_etime = item.value[1];
          let timer = ["", ""];
          if (JSON.stringify(item.value) !== JSON.stringify(["", ""])) {
            timer = [
              this.$Date(item.value[0]).format("YYYY-MM-DD"),
              this.$Date(item.value[1]).format("YYYY-MM-DD"),
            ];
          }
          this.storage.set("publicList", timer);
        } else if (item.id === 31) {
          this.params.created_stime = item.value[0];
          this.params.created_etime = item.value[1];
          let timer2 = ["", ""];
          if (JSON.stringify(item.value) !== JSON.stringify(["", ""])) {
            timer2 = [
              this.$Date(item.value[0]).format("YYYY-MM-DD"),
              this.$Date(item.value[1]).format("YYYY-MM-DD"),
            ];
          }
          this.storage.set("publicList2", timer2);
        } else if (item.id === 3) {
          this.params.channel_id = item.value;
        } else if (item.id === 4) {
          this.params.patient_status_id = item.value;
        } else if (item.id === 6) {
          this.params.department_id = item.value;
        } else if (item.id === 10) {
          this.params.patient_diseases_id = item.value;
        } else if (item.id === 11) {
          this.params.last_callback_id = item.value;
        } else if (item.id === 15) {
          this.params.last_callback_state_id = item.value;
        } else if (item.id === 12) {
          this.params.dialog_mode_id = item.value;
        } else if (item.id === 21) {
          this.params.have_contact_type = item.value;
        }
      });
      this.askData();
    },
    askDatachangeing() {
      this.params.page = 1;
      this.searchData.project.forEach((item) => {
        if (item.id === 1) {
          this.params.export_stime = item.value[0];
          this.params.export_etime = item.value[1];
          let timer = ["", ""];
          if (JSON.stringify(item.value) !== JSON.stringify(["", ""])) {
            timer = [
              this.$Date(item.value[0]).format("YYYY-MM-DD"),
              this.$Date(item.value[1]).format("YYYY-MM-DD"),
            ];
          }
          this.storage.set("publicList", timer);
        } else if (item.id === 31) {
          this.params.created_stime = item.value[0];
          this.params.created_etime = item.value[1];
          let timer2 = ["", ""];
          if (JSON.stringify(item.value) !== JSON.stringify(["", ""])) {
            timer2 = [
              this.$Date(item.value[0]).format("YYYY-MM-DD"),
              this.$Date(item.value[1]).format("YYYY-MM-DD"),
            ];
          }
          this.storage.set("publicList2", timer2);
        } else if (item.id === 3) {
          this.params.channel_id = item.value;
        } else if (item.id === 4) {
          this.params.patient_status_id = item.value;
        } else if (item.id === 6) {
          this.params.department_id = item.value;
        } else if (item.id === 10) {
          this.params.patient_diseases_id = item.value;
        } else if (item.id === 11) {
          this.params.last_callback_id = item.value;
        } else if (item.id === 15) {
          this.params.last_callback_state_id = item.value;
        } else if (item.id === 21) {
          this.params.have_contact_type = item.value;
        } else if (item.id === 12) {
          this.params.dialog_mode_id = item.value;
        }
      });
      this.changetag(
        this.params.belong_member_id ? this.params.belong_member_id : undefined
      );
    },
    askDatachange() {
      this.params.page = 1;
      this.params.belong_member_id = "";
      this.searchData.project.forEach((item) => {
        if (item.id === 1) {
          this.params.export_stime = item.value[0];
          this.params.export_etime = item.value[1];
        } else if (item.id === 31) {
          this.params.created_stime = item.value[0];
          this.params.created_etime = item.value[1];
        } else if (item.id === 3) {
          this.params.channel_id = item.value;
        } else if (item.id === 4) {
          this.params.patient_status_id = item.value;
        }
        // else if (item.id === 5) {
        //     this.params.belong_member_id = item.value;
        // }
        else if (item.id === 6) {
          this.params.department_id = item.value;
        } else if (item.id === 21) {
          this.params.have_contact_type = item.value;
        }
      });
      if (this.params.department_id) {
        this.member_list_list = [];
        for (let its in this.member_list) {
          if (this.params.department_id === this.member_list[its].value) {
            this.member_list_list = [this.member_list[its]];
            this.showkey = true;
          }
        }
      } else if (!this.params.department_id) {
        this.showkey = false;
      }
      this.askData();
    },
    askDatachangess() {
      this.params.page = 1;
      this.searchData.project.forEach((item) => {
        if (item.id === 9) {
          this.params.tags_ids = item.value.join(",");
        }
      });
      this.askData();
    },
    askDataclick() {
      if (!this.realnamebool) {
        return this.$Message.error("上次归属人不一致，请重新选择");
      }
      if (this.formItem.allot_ids === "") {
        return this.$Message.error("请选择患者");
      }
      this.modal = true;
    },
    sortchange(e) {
      if (e.key === "export_time_str") {
        if (e.order === "desc") {
          this.params.sort_export_time = 2;
        } else if (e.order === "asc") {
          this.params.sort_export_time = 1;
        } else {
          this.params.sort_export_time = 0;
        }
        this.params.sort_callback_count = 0;
        this.params.sort_last_callback_time = 0;
      } else if (e.key === "callback_count") {
        if (e.order === "desc") {
          this.params.sort_callback_count = 2;
        } else if (e.order === "asc") {
          this.params.sort_callback_count = 1;
        } else {
          this.params.sort_callback_count = 0;
        }
        this.params.sort_export_time = 0;
        this.params.sort_last_callback_time = 0;
      } else {
        if (e.order === "desc") {
          this.params.sort_last_callback_time = 2;
        } else if (e.order === "asc") {
          this.params.sort_last_callback_time = 1;
        } else {
          this.params.sort_last_callback_time = 0;
        }
        this.params.sort_export_time = 0;
        this.params.sort_callback_count = 0;
      }
      this.askData();
    },
    genderfun(type) {
      let gender = "";
      if (+type === 0) gender = "未知";
      else if (+type === 1) gender = "男";
      else if (+type === 2) gender = "女";
      return gender;
    },
    handleOpenDrawer(row) {
      if (row === -1) this.$refs.editPatient.handleOpenDrawer();
      else this.$refs.editPatient.handleEdit(row, true);
    },
    handleReturnVisit(id) {
      this.$refs.returnVisit.handleReturnVisit(id);
    },
    handleDetailPatient(row) {
      this.$refs.patientDetail.handleOpen(row, true);
    },
    selecttanle(e) {
      let list = [];
      let realnametext = "";
      if (e.length > 0) realnametext = e[0].realname;
      this.realnamebool = true;
      for (let item of e) {
        list.push(item.id);
        if (this.realnamebool) {
          this.realnamebool = realnametext === item.realname;
        }
      }
      this.formItem.allot_ids = list.join(",");
    },
    handleSubmit() {
      this.remarksloading = true;
      let config = {
        allot_member_ids: this.formItem.allot_member_ids.join(","),
        allot_ids: this.formItem.allot_ids,
      };
      patientindexseaallot(config)
        .then(() => {
          this.$Message.success("分配成功");
          this.modal = false;
          this.askData();
          setTimeout(() => {
            this.remarksloading = false;
          }, 500);
        })
        .catch(() => {
          this.remarksloading = false;
        });
    },
    handleDrawerCancel() {
      // this.formItem = { allot_member_ids: [], allot_ids: "" };
      this.modal = false;
    },

    handleFilterChange(value) {
      let params = {
        not_callback_start: "",
        not_callback_end: this.$Date().format("YYYY-MM-DD"),
      };
      if (+value._filterChecked[0] === 1) {
        params.not_callback_start = this.$Date()
          .subtract(10, "day")
          .format("YYYY-MM-DD");

        params.not_callback_end = this.$Date().format("YYYY-MM-DD");
      } else if (+value._filterChecked[0] === 2) {
        params.not_callback_start = this.$Date()
          .subtract(20, "day")
          .format("YYYY-MM-DD");

        params.not_callback_end = this.$Date()
          .subtract(11, "day")
          .format("YYYY-MM-DD");
      } else if (+value._filterChecked[0] === 3) {
        params.not_callback_start = this.$Date()
          .subtract(30, "day")
          .format("YYYY-MM-DD");

        params.not_callback_end = this.$Date()
          .subtract(21, "day")
          .format("YYYY-MM-DD");
      } else if (+value._filterChecked[0] === 4) {
        params.not_callback_start = this.$Date()
          .subtract(60, "day")
          .format("YYYY-MM-DD");

        params.not_callback_end = this.$Date()
          .subtract(31, "day")
          .format("YYYY-MM-DD");
      } else if (+value._filterChecked[0] === 5) {
        params.not_callback_start = "";
        params.not_callback_end = this.$Date()
          .subtract(61, "day")
          .format("YYYY-MM-DD");
      } else {
        params.not_callback_start = "";
        params.not_callback_end = "";
      }
      this.params.page = 1;
      this.params.not_callback_end = params.not_callback_end;
      this.params.not_callback_start = params.not_callback_start;
      this.askData();
    },
    remoteMethod(query) {
      clearTimeout(this.timer);
      if (query !== "") {
        let reg = new RegExp("[\\u4E00-\\u9FFF]+", "g");
        if (!reg.test(query)) {
          return;
        }
        this.loadingcity = true;
        return new Promise((resolve) => {
          this.timer = setTimeout(() => {
            patientindexsearchcity({
              keywords: query,
            }).then((res) => {
              this.query = "";
              if (res.list && res.list.length > 0) {
                let list = [];
                for (let item of res.list) {
                  list.push({
                    value: item.value,
                    label: item.label,
                  });
                }
                this.optionscity = list;
              }
              resolve(res.data);
            });
            this.loadingcity = false;
          }, 200);
        });
      }
    },
    getcityDatas() {
      this.askData();
    },

    checkbox1(e) {
      this.params.page = 1;
      this.params.have_mobile = e.length;
      this.searchData.project.forEach((item) => {
        if (item.id === 1) {
          item.value = ["", ""];
          this.params.export_stime = "";
          this.params.export_etime = "";
        }
        if (item.id === 31) {
          item.value = ["", ""];
          this.params.created_stime = "";
          this.params.created_etime = "";
        }
      });
      this.askData();
    },
    checkbox2(e) {
      this.params.page = 1;
      this.params.have_wechat = e.length;
      this.searchData.project.forEach((item) => {
        if (item.id === 1) {
          item.value = ["", ""];
          this.params.export_stime = "";
          this.params.export_etime = "";
        }
        if (item.id === 31) {
          item.value = ["", ""];
          this.params.created_stime = "";
          this.params.created_etime = "";
        }
      });
      this.askData();
    },
    checkbox3(e) {
      this.params.page = 1;
      // this.searchData.project.forEach((item) => {
      //     if (item.id === 1) {
      //         item.value = ['', ''];
      //         this.params.export_stime = '';
      //         this.params.export_etime = '';
      //     }
      //     if (item.id === 31) {
      //         item.value = ['', ''];
      //         this.params.created_stime = '';
      //         this.params.created_etime = '';
      //     }
      // });
      this.askData();
    },
  },
  watch: {
    $route() {
      this.$refs.returnVisit.handleDrawerCancel("formValidate");
    },
  },
};
</script>
